package com.situ.blog.servlet;

import com.situ.blog.listener.BlogExcelListener;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.BlogType;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.service.IBlogService;
import com.situ.blog.service.IBlogTypeService;
import com.situ.blog.service.impl.BlogSeviceImpl;
import com.situ.blog.service.impl.BlogTypeServiceImpl;
import com.situ.blog.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    IBlogService blogService = new BlogSeviceImpl();
    IBlogTypeService blogTypeService = new BlogTypeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method==null||method.equals("")){
            method = "selectByPage";
        }
        switch (method){
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "updateStatus":
                updateStatus(req,resp);
                break;
            case "exportExcel":
                exportExcel(req,resp);
                break;
            case "importExcel":
                importExcel(req,resp);
                break;
        }
    }

    private void importExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.importExcel");
        blogService.importExcel(req);
        JSONUtil.toJSON(resp,Result.ok("导入成功"));
    }

    private void exportExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.exportExcel");
        //调用service层
        List<Blog> list = blogService.selectAll();
        ExcelUtil.exportExcel(resp,list, Blog.class,"blog");
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.deleteAll");
        //传过来的是一个id的数组
        String[] ids = req.getParameterValues("ids[]");
        System.out.println(ids);
        //收到了传过来的id值，然后调用service
        blogService.deleteAll(ids);
        Result result = new Result(0,"删除成功");
        JSONUtil.toJSON(resp,result);
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        blogService.updateStatus(id,Integer.parseInt(status));
        JSONUtil.toJSON(resp,Result.ok("更新成功"));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.update");
        Blog blog = MyBeanUtil.copyToBean(req, Blog.class);
        blogService.update(blog);
        Result result = new Result(0,"修改成功");
        JSONUtil.toJSON(resp,result);
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.selectById");
        String id = req.getParameter("id");
        List<BlogType> list = blogTypeService.selectAll();
        Blog blog = blogService.selectById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("blog",blog);
        System.out.println("servlet中:"+blog+"and list:"+list);
        JSONUtil.toJSON(resp,Result.ok(map));

    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.add");
        String name = req.getParameter("name");
        System.out.println(name);
        //封装一下

        Blog blog = MyBeanUtil.copyToBean(req, Blog.class);
        System.out.println(blog);
        blogService.add(blog);
        //返回Ajax数据
        Result result = new Result(0,"添加成功");
        JSONUtil.toJSON(resp,result);
    }

    /*private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        //得到数组之后调用service层函数
        blogService.deleteAll(ids);
        //然后返回json数据
        Result result = new Result(0,"删除成功");
        JSONUtil.toJSON(resp,result);
        *//*resp.setContentType("text/html;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getWriter(), result);*//*
    }*/

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogServlet.deleteById");
        //接收参数
        String id = req.getParameter("id");
        System.out.println("id:"+id);
        //调用函数
        int count = blogService.deleteById(Integer.parseInt(id));//没有返回值
        Result result = null;
        if (count>0){
            result = new Result(0,"删除成功");
        }else {
            result = new Result(1,"删除失败");
        }
        //固定的三行
        JSONUtil.toJSON(resp,result);
        //*resp.setContentType("text/html;charset=UTF-8");
        //ObjectMapper mapper = new ObjectMapper();
       //mapper.writeValue(resp.getWriter(), result);//*//现在不知道这里要返回什么
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("BlogServlet.selectByPage");
        /*String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");*/
        //1.封装所需数据page:当前页码 ;limit:每页的信息数 ;name:查找的课程名字 ;credit:查找的学分

        //调用位于service层的分页查找功能
        BlogQuery blogQuery = MyBeanUtil.copyToBean(req, BlogQuery.class);
        System.out.println(blogQuery);
        PageRusult pageResult = blogService.selectByPage(blogQuery);

        JSONUtil.toJSON(resp, pageResult);

    }
}
